
package com.scjyzr.mysweeper;

import java.util.Timer;
import java.util.TimerTask;

public class MyTimer {

    private Timer timer;

    private MyTimerTask task;

    private MyCallBack myCallBack;

    private int m_nTime = 0;// 计时

    public MyTimer(MyCallBack context) {
        this.myCallBack = context;
        

    }

    /**
     * 功能说明:启动定时器。
     * 
     * @date 2013-8-8
     * @author suchangjun
     * @since v1.0
     * @throws null
     */
    public void startTimer() {
        task = new MyTimerTask();
        timer = new Timer(true);
        timer.scheduleAtFixedRate(task, 0, 1000);
    }

    public void stopTimer() {
        timer.cancel();
        timer.purge();
        m_nTime=0;
    }
    
    public void onPause() {
        timer.cancel();
        timer.purge();
    }

    public void destory() {
        stopTimer();
        m_nTime=0;
        timer = null;
        task = null;
        myCallBack = null;
    }

    private class MyTimerTask extends TimerTask {

        public void run() {
            m_nTime++;
            if (myCallBack != null) {
                myCallBack.myCallBack(m_nTime,MyCallBack.IS_TIME);
            }
        }
    }

}
